package com.zerowidth.db.op;

import com.zerowidth.db.DBHelper;
import com.zerowidth.db.base.SqlResult;
import com.zerowidth.db.bean.YiYuanDuoBaoBean;
import com.zerowidth.db.utils.IOUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

public class YiYuanDuoBaoDB {

    /**
     * 查询最新一期、且未结束的 夺宝数据
     */
    public static YiYuanDuoBaoBean findLastOpenDuobao() {
        String sql = String.format("SELECT * FROM yiyuanduobao_main WHERE has_over = 0 and disable = 0 order by create_time desc");

        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            ResultSet rs = sr.rs;
            if (rs.next()) {
                YiYuanDuoBaoBean bean = new YiYuanDuoBaoBean();
                bean.id = rs.getInt("id");
                bean.duobao_index = rs.getInt("duobao_index");
                bean.award_id = rs.getInt("award_id");
                bean.award_name = rs.getString("award_name");
                bean.award_desc = rs.getString("award_desc");
                bean.total = rs.getInt("total");
                bean.sale_count = rs.getInt("sale_count");
                bean.price = rs.getInt("price");
                bean.user_id = rs.getString("taker_user_id");
                bean.hasOver = rs.getInt("has_over") == 1;
                bean.create_time = rs.getString("create_time");

                return bean;
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }

    public static YiYuanDuoBaoBean findDuobaoById(int id) {
        String sql = String.format("SELECT * FROM yiyuanduobao_main WHERE id = " + id);

        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            ResultSet rs = sr.rs;
            if (rs.next()) {
                YiYuanDuoBaoBean bean = new YiYuanDuoBaoBean();
                bean.id = rs.getInt("id");
                bean.duobao_index = rs.getInt("duobao_index");
                bean.award_id = rs.getInt("award_id");
                bean.award_name = rs.getString("award_name");
                bean.award_desc = rs.getString("award_desc");
                bean.total = rs.getInt("total");
                bean.sale_count = rs.getInt("sale_count");
                bean.price = rs.getInt("price");
                bean.user_id = rs.getString("taker_user_id");
                bean.hasOver = rs.getInt("has_over") == 1;
                bean.create_time = rs.getString("create_time");

                return bean;
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }
}
